package com.horizon.common.util;

import com.horizon.common.repository.HQLInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 字符串操作工具类
 *
 * @author wangfuwei
 * @version 2018-08-09
 */
@SuppressWarnings("unused")
public class StringUtil {

    /**
     * 是否为空字符串
     *
     * @param str 判断对象
     */
    public static Boolean isEmpty(Object str) {
        return str == null || "".equals(str);
    }

    /**
     * 是否为非空字符串
     *
     * @param str 判断对象
     */
    public static Boolean isNotEmpty(Object str) {
        return !isEmpty(str);
    }

    /**
     * 封装分页查询HQL基本信息
     *
     * @param request HttpServletRequest请求
     */
    public static HQLInfo pageHQL(HttpServletRequest request) {
        String s_pageno = request.getParameter("pageno");
        String s_rowsize = request.getParameter("rowsize");
        String orderby = request.getParameter("orderby");
        int pageno = 0;
        int rowsize = 0;

        if (s_pageno != null && s_pageno.length() > 0) {
            pageno = Integer.parseInt(s_pageno);
        }
        if (s_rowsize != null && s_rowsize.length() > 0) {
            rowsize = Integer.parseInt(s_rowsize);
        }
        HQLInfo hqlInfo = new HQLInfo();
        hqlInfo.setOrderBy(orderby);
        hqlInfo.setPageNo(pageno);
        hqlInfo.setRowSize(rowsize);

        return hqlInfo;
    }

    /**
     * 是否响应页面，否则返回JSON
     *
     * @param request HttpServletRequest请求
     */
    public static Boolean htmlView(HttpServletRequest request) {
        String accept = request.getHeader("Accept");
        return accept.startsWith("text/html");
    }
}
